<?
/**
*  报表数据处理
*  2013/11/23 by zh
*/

include("../webkit/include/simple_db.php");

include("session.php");
require("ajax_base.php");

$db = new SimpleDB();
$action = $_GET['action'];

if($action == "agents_report") {
	$sql = "select sum(order_count) as agent_sum, client_source from hncc_client where 1=1 group by client_source";
	$sql = add_condition($sql);
	$db->query($sql);
	$label = array(1=>"电信渠道", 2=>"三方代理");
	$total = 0;
	while($db->moveNext()) {
		$data[] = "{'label':'".$label[$db->f("client_source")]."','value':'".$db->f("agent_sum")."'}";
		$total += $db->f("agent_sum");
	}
	print_json("【合作分成总数($total)个】", "A", "B", $data);
}
else if($action == "provinces_report") {
	$sql = "select sum(order_count) as agent_sum, client_area from hncc_client where 1=1 group by client_area";
	$sql = add_condition($sql);
	$db->query($sql);
	$total = 0;
	while($db->moveNext()) {
		$data[] = "{'label':'".$db->f("client_area")."','value':'".$db->f("agent_sum")."','link':'j-show_client_list_by_area-".$db->f("client_area")."'}";
		$total += $db->f("agent_sum");
	}
	print_json("【业务区域分布($total)个】", "A", "B", $data);	
}
else if($action == "money_report") {
	//$month_total = "ROUND(DATEDIFF(STR_TO_DATE('".$_GET['date_end']."', '%Y-%m-%d'), STR_TO_DATE('".$_GET['date_start']."', '%Y-%m-%d'))/30)"; // 计算从签约时间到截时间的月份
	$date_range = array($_GET['date_start'], $_GET['date_end']);
	$month_total = "IF(order_date < STR_TO_DATE('".$date_range[0]."', '%Y-%m-%d'),  "; // 比较起始时间和签约时间; 2014/4/28
	$month_total .= "ROUND(DATEDIFF(STR_TO_DATE('".$date_range[1]."', '%Y-%m-%d'), STR_TO_DATE('".$date_range[0]."', '%Y-%m-%d'))/30), "; // 计算时间范围内的月份
	$month_total .= "ROUND(DATEDIFF(STR_TO_DATE('".$date_range[1]."', '%Y-%m-%d'), order_date)/30))"; // 计算从签约时间到截时间的月份

	$sql = "select sum(order_count*order_price*$month_total) as total_sum, sum(CASE client_source WHEN 1 THEN 0.3 ELSE 0.6 END * order_count*order_price*$month_total) as si_sum, sum(order_count*order_price*money_rate*$month_total) as money_sum, sum(deve_price) as deve_sum from hncc_client where 1=1";
	$sql = add_condition($sql);
	$db->query($sql);
	$total = 0;
	$db->moveNext();
	$total_sum = round($db->f("total_sum"));
	$si_sum = round($db->f("si_sum"));
	$money_sum = round($db->f("money_sum"));
	$deve_sum = round($db->f("deve_sum"));
	$telcom_sum = $total_sum - $si_sum; // 电信收入=总收入-SI分成
	$data[] = "{'label':'电信收入','value':'".$telcom_sum."'}";
	$si_sum = $si_sum  - $money_sum;   // 新太收入=SI分成-代理分成
	$data[] = "{'label':'SI分成','value':'".$si_sum."'}";
	$data[] = "{'label':'代理分成','value':'".$money_sum."'}";
	//$data[] = "{'label':'定制开发','value':'".$deve_sum."'}";
	print_json("【业务收入总计(".($total_sum+$deve_sum).")元】", "A", "B", $data); // 总收入里要包括定制开发费
}
else if($action == "service_type_report") { // 业务类型
	$sql = "select sum(order_count) as agent_sum, service_type from hncc_client where 1=1 group by service_type";
	$sql = add_condition($sql);
	//echo $sql;
	$db->query($sql);
	$total = 0;
	while($db->moveNext()) {
		$data[] = "{'label':'".$dic_data['service_type'][$db->f("service_type")]."','value':'".$db->f("agent_sum")."'}";
		$total += $db->f("agent_sum");
	}
	print_json("【业务类型统计($total)个】", "A", "B", $data);	
}
else if($action == "client_type_report") { // 行业类型
	$sql = "select sum(order_count) as agent_sum, client_type from hncc_client where 1=1 group by client_type";
	$sql = add_condition($sql);
	//echo $sql;
	$db->query($sql);
	$total = 0;
	while($db->moveNext()) {
		$data[] = "{'label':'".$db->f("client_type")."','value':'".$db->f("agent_sum")."'}";
		$total += $db->f("agent_sum");
	}
	print_json("【行业类型统计($total)个】", "A", "B", $data);	
}

function add_condition($src_sql) {
	$condition = "client_status=1";
	if(isset($_GET['date_end'])) {
		$condition .= " and order_date < '".$_GET['date_end']."'"; // 这里只要判断签约日期在限之内即可; 2014/3/28
	}
	$_SESSION['report_date_range'] = $_GET['date_start']."_".$_GET['date_end']; // 保存一下时间段，以备链接报表输出
	$src_sql = str_replace("1=1", $condition, $src_sql);
	return $src_sql;
}


function print_json($title, $xname, $yname, $data) {
	echo "{";
	echo "'chart': {";
	echo "'caption' : '$title',";
	echo "'xAxisName' : '$xname',";
	echo "'yAxisName' : '$yname',";
	echo "'formatNumberScale' : '0',";
	if($xname == "CHANNEL")  {
		// for Bar2D (通道统计明细)
		//---------------------------------
		echo "'alternateVGridColor' : 'AFD8F8',";
		echo "'toolTipBorderColor' : '114B78',";
		echo "'baseFontColor' : '114B78',";
		echo "'toolTipBgColor' : 'E7EFF6',";
		echo "'plotBorderDashed' : '1',";
		echo "'plotBorderDashLen' : '2',";
		echo "'useRoundEdges' : '1',";   
		echo "'plotBorderDashGap' : '2',"; 
		echo "'showBorder' : '0',";   
		echo "'bgColor' : 'FFFFFF,FFFFFF',";   
		//---------------------------------
	}
	echo "'baseFontSize' : '12'},";        
	echo "'data' : [";               
	echo join(", ", $data);
	echo "]";
	echo "}";
}


